####
#### Eroll Kuhn & Rahsaan Maxwell
#### Non Linearity: Demographics



rm(list=ls())

library(tidyverse)
library(gridExtra)

# read in frame
df <- read.csv(".../5m. Robustness, Nonlinearity/Output/B.Tables/Clean_Predictions.csv")

df$facet_lab <- ifelse(df$Var=="% Foreign-Born",1,
                       ifelse(df$Var=="% non-EU Foreign-Born",2,
                              #ifelse(df$Var=="% from Arabic-Speaking Country",3,
                              ifelse(df$Var=="% Co-National (Syrians Only)",3,NA)))



df$facet_lab <- factor(df$facet_lab,
                       levels = c(1,2,3),
                       labels = c("% Foreign-Born", 
                                  "% non-EU Foreign-Born",
                                  "% Co-National (Syrians Only)"))

setwd(".../5m. Robustness, Nonlinearity/Output")
png("NonLinearity_Demographics.png",
    width=10,height=6,units='in',res=300)
ggplot(df) +
  geom_line(aes(x=Value,y=Pred),size=1) +
  geom_point(aes(x=Value,y=Pred),size=2) +
  geom_smooth(aes(x=Value,y=bottom95),color="red",size=0.5,lty=3) +
  geom_smooth(aes(x=Value,y=top95),color="red",size=0.5,lty=3) +
  geom_smooth(aes(x=Value,y=bottom95),color="red",size=0.5,lty=3) +
  geom_smooth(aes(x=Value,y=top95),color="red",size=0.5,lty=3) +
  facet_wrap(~facet_lab) +
  theme_bw() + 
  geom_hline(yintercept = 0, lty = 2) +
  geom_hline(yintercept = 1, lty = 2) +
  xlab("Value of Demographic Variable (% of County Population)") +
  ylab("Predicted Value on Welcomeness Index") +
  theme(legend.position = "none",
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank())
dev.off()

